package com.spring.property.config;

import com.spring.property.model.Student;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 每一行代码，都是改变世界的力量
 *
 * @author 郭帅
 * @date 2022/11/16 17:18
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//        BeanDefinition bd = new GenericBeanDefinition();
//        bd.setBeanClassName("com.spring.property.model.Student");

        BeanDefinition bd = BeanDefinitionBuilder.genericBeanDefinition(Student.class, () -> {
            {
                Student student = new Student();
                student.setName("guoshuai");
                return student;
            }
        }).getBeanDefinition();

        registry.registerBeanDefinition("student", bd);
        System.out.println("postProcessBeanDefinitionRegistry");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("============= BeanDefinitionRegistryPostProcessorTest invoke beanFactory =========");
        String[] strings = beanFactory.getBeanDefinitionNames();
        System.out.println("postProcessBeanFactory----start");
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("postProcessBeanFactory----end");

    }
}
